package me.ingxin.android.permission2

import android.content.Context
import androidx.fragment.app.FragmentManager
import me.ingxin.android.permission2.ext.BackgroundBodySensorsPermissionsTask
import me.ingxin.android.permission2.ext.BackgroundLocationPermissionTask
import me.ingxin.android.permission2.ext.InstallPackagesPermissionTask
import me.ingxin.android.permission2.ext.ManageExternalStoragePermissionTask
import me.ingxin.android.permission2.ext.NotificationPermissionTask
import me.ingxin.android.permission2.ext.SystemAlertWindowPermissionTask
import me.ingxin.android.permission2.ext.WriteSettingsPermissionTask
import me.ingxin.android.permission2.task.FinallyTask
import me.ingxin.android.permission2.task.NormalPermissionsTask
import me.ingxin.android.permission2.request.TaskChain
import me.ingxin.android.permission2.request.Request
import me.ingxin.android.permission2.strategy.ReasonStrategy
import me.ingxin.android.permission2.task.PermissionResultChecker
import me.ingxin.android.permission2.task.NormalPermissionsReasonTask
import me.ingxin.android.permission2.view.Guide
import me.ingxin.android.permission2.view.impl.DefaultGuide
import me.ingxin.android.permission2.view.impl.SimpleGuide

/**
 * Created on 2023/7/1 by yin
 * @param normalPermissions 需要请求的常规权限
 * @param specialPermissions 需要请求的特殊权限
 */
class PermissionMediator internal constructor(
    private val context: Context,
    private val fragmentManager: FragmentManager,
    internal val normalPermissions: List<String>,
    internal val specialPermissions: List<String>
) {

    private val defaultGuid by lazy { DefaultGuide() }

    @JvmField
    internal var reasonStrategy = ReasonStrategy.Before

    @JvmField
    internal var settings = true

    @JvmField
    internal var tipsWhileRequest = true

    private var guide: Guide? = null

    internal fun getGuide(): Guide = guide ?: defaultGuid

    /**
     * 申请权限时，理由对话框出现的时机
     * @param strategy 默认为[ReasonStrategy.Before]
     */
    fun reasonStrategy(strategy: ReasonStrategy) = apply { reasonStrategy = strategy }

    /**
     * 申请权限失败时，是否引导用户前往设置页面（启动单独页面开启的权限无效）
     * @param enable true-开启，false-关闭，默认开启
     */
    fun toSettings(enable: Boolean) = apply { settings = enable }

    /**
     * 自定义引导弹窗
     */
    fun setGuide(guide: Guide) = apply { this.guide = guide }

    /**
     * 自定义引导窗信息，该方法与[setGuide]相互覆盖，最后设置的有效
     * @param tipsBlock [tipsWhileRequest]显示的消息
     * @param reasonBlock 理由或者跳转设置时展示的消息
     */
    fun message(
        reasonBlock: ((List<String>, Boolean) -> CharSequence?)? = null,
        tipsBlock: ((List<String>) -> CharSequence?)? = null
    ) = apply {
        if (reasonBlock != null || tipsBlock != null) {
            setGuide(SimpleGuide(tipsBlock, reasonBlock))
        }
    }

    /**
     *
     * 申请常规权限的同时是否显示一个说明框
     * @param show true显示，false不显示，默认显示
     */
    fun tipsWhileRequest(show: Boolean) = apply { tipsWhileRequest = show }

    /**
     * 发起权限申请
     */
    fun request(callback: Callback) {
        val checker = PermissionResultChecker(callback)
        val taskList = listOf(
            NormalPermissionsReasonTask(),
            NormalPermissionsTask(),
            BackgroundLocationPermissionTask(),
            InstallPackagesPermissionTask(),
            WriteSettingsPermissionTask(),
            SystemAlertWindowPermissionTask(),
            ManageExternalStoragePermissionTask(),
            NotificationPermissionTask(),
            BackgroundBodySensorsPermissionsTask(),
            FinallyTask(checker)
        )
        val chain = TaskChain(context, fragmentManager, taskList, 0)
        chain.proceed(Request.Builder(this).build())
    }


}